package rp.lee.callsdecline.core.db;

import rp.lee.callsdecline.utils.BaseException;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.SyncStateContract.Columns;

public abstract class DBBase<T, K> implements DBTable<T, K> {
	
	protected SQLiteHelper mSQLiteHelper;
	protected Context mContext;
	protected SQLiteDatabase db;
	
	public DBBase(Context mContext, boolean ifNotExistCreate) {
		this.mContext = mContext;
		open();
		if(ifNotExistCreate && !mSQLiteHelper.tableIsExist(getTableName())){
			db.execSQL(getCreateTableSql());
		}
		close();
	}
	
	@Override
	public boolean delete(K k) throws BaseException {
		int row = db.delete(getTableName(), Columns._ID + "=?", new String[]{String.valueOf(k)});
		return row > 0;
	}
	
	public void close() {
		mSQLiteHelper.close();
	}

	public void open() {
		mSQLiteHelper = new SQLiteHelper(mContext);
		db = mSQLiteHelper.getWritableDatabase();
	}
}
